<template>
  <a-modal
    title="设置优惠券回调地址"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="服务商名称">
          <a-input :disabled="true" placeholder="服务商名称"
                   v-decorator="[ 'name', {rules: [] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="回调地址">
          <a-input placeholder="回调地址"
                   v-decorator="[ 'notifyUrl', {rules: [{ required: true, message: '必输', whitespace: true}] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="结算金额">
          <a-radio-group buttonStyle="solid" v-decorator="[ 'isSwitch', {rules: [{ required: true, message: '请选择' }], initialValue: true}]">
            <a-radio-button :value="true">开启</a-radio-button>
            <a-radio-button :value="false">关闭</a-radio-button>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { updateNotifyUrl } from '@/api/system/company'

  export default {
    data () {
      return {
        id: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        isDisabled: false,
        treeData: [],
        roleList: [],

        form: this.$form.createForm(this)
      }
    },
    mounted () {
    },
    methods: {
      edit (record) {
        this.id = record.id
        this.visible = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['name', 'notifyUrl', 'isSwitch'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            updateNotifyUrl(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        this.isDisabled = false
        this.form.resetFields()
        this.id = null
      },

    }
  }
</script>
